package com.project.webservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.webservice.dto.WebServiceR;
import com.project.webservice.entity.TUser;
import com.project.webservice.entity.TUserFunc;
import com.project.webservice.entity.TUserXl;
import com.project.webservice.mapper.TUserFuncMapper;
import com.project.webservice.mapper.TUserMapper;
import com.project.webservice.mapper.TUserXlMapper;
import com.project.webservice.service.TUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Resource
    private TUserFuncMapper tUserFuncMapper;
    @Resource
    private TUserXlMapper tUserXlMapper;
    @DS("rfid")
    public List<TUser> selectAllUsers(){
        log.info("selectAllUsers");
        return baseMapper.selectAllUsers();
    }

    @DS("rfid")
    public int saveRfid(TUser user){
        if(StringUtils.isNotBlank(user.getUid())) {
            if (super.getById(user.getUid()) != null) {
                return 1;
            }
        }
        if (super.save(user)) {
           return 0;
        } else {
            return 2;
        }

    }
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @DS("rfid")
    public void proRemoveUserByUID(String uid) throws Exception{
        log.info("proRemoveUserByUID:{}",uid);
        tUserFuncMapper.delete(Wrappers.<TUserFunc>lambdaQuery().eq(TUserFunc::getUid, uid));//删除t_user_func
        tUserXlMapper.delete(Wrappers.<TUserXl>lambdaQuery().eq(TUserXl::getUid, uid)); //删除t_user_xl
        super.removeById(uid); //删除t_user

    }

}
